import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    _filter="Image File(*.jpg *.png *.ico);;Python File(*.py)"      
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        
        toolBar=QToolBar()
        btn=QPushButton('选择图片(静态方法)')
        btn.clicked.connect(self.GetFileStatic)
        toolBar.addWidget(btn)

        btn=QPushButton('选择图片(实例态方法)')
        btn.clicked.connect(self.GetFileInstance)
        toolBar.addWidget(btn)

        btn=QPushButton('选择图片(实例态方法)')
        btn.clicked.connect(self.GetFileInstance2)
        toolBar.addWidget(btn)

        btn=QPushButton('选择多个图片(静态方法)')
        btn.clicked.connect(self.GetMultiFileStatic)
        toolBar.addWidget(btn)

        btn=QPushButton('选择多个图片(实例方法)')
        btn.clicked.connect(self.GetMultiFileInstance)
        toolBar.addWidget(btn)

        btn=QPushButton('选择文件夹(静态方法)')
        btn.clicked.connect(self.GetFolderStatic)
        toolBar.addWidget(btn)

        btn=QPushButton('选择文件夹(实例方法)')
        btn.clicked.connect(self.GetFolderInstance)
        toolBar.addWidget(btn)

        btn=QPushButton('获取存储路径')
        btn.clicked.connect(self.GetSavePath)
        toolBar.addWidget(btn)
        layout.addWidget(toolBar)

        self.label=QLabel('选择结果')
        layout.addWidget(self.label)

        self.setLayout(layout)
    def GetFileStatic(self):
       filename,filter= QFileDialog.getOpenFileName(self,caption="Selecte One File",dir=self.parent.getAppDir(),filter=self._filter)       
       print(filter)
       self.label.setText(f'你选择的文件:\n{filename}')
    def GetFileInstance(self):
        dialog=QFileDialog(self,caption='Select One File',directory=self.parent.getAppDir(),filter=self._filter)
        if dialog.exec():
            files=dialog.selectedFiles()
            self.label.setText(f'你选择的文件:\n{files[0]}')
    def GetFileInstance2(self):
        dialog=QFileDialog()
        dialog.setWindowTitle('Select One File')
        dialog.setDirectory(self.parent.getAppDir())
        dialog.setNameFilter(self._filter)
        if dialog.exec():
            files=dialog.selectedFiles()
            self.label.setText(f'你选择的文件:\n{files[0]}')
    def GetMultiFileStatic(self):
       files,filter= QFileDialog.getOpenFileNames(self,caption="Selecte Multi File",dir=self.parent.getAppDir(),filter=self._filter)       
       print(filter)
       self.label.setText('你选择的文件:\n'+';\n'.join(files))
    def GetMultiFileInstance(self):
        dialog=QFileDialog(self,caption='Select Multi File',directory=self.parent.getAppDir(),filter=self._filter)
        dialog.setFileMode(QFileDialog.FileMode.ExistingFiles)
        if dialog.exec():
            files=dialog.selectedFiles()
            self.label.setText('你选择的文件:\n'+';\n'.join(files))
    def GetFolderStatic(self):
        dialog=QFileDialog(self,caption='Select One Directory',directory=self.parent.getAppDir(),filter=self._filter)
        dialog.setFileMode(QFileDialog.FileMode.Directory)
        if dialog.exec():
            files=dialog.selectedFiles()
            self.label.setText('你选择的路径:\n'+';\n'.join(files))
    def GetFolderInstance(self):
        dir=QFileDialog.getExistingDirectory(self,caption='Select One Directory',dir=self.parent.getAppDir())
        self.label.setText(f'你选择的路径:\n{dir}')
    def GetSavePath(self):
        dir,x=QFileDialog.getSaveFileName(self,caption='Get Save Directory',dir=self.parent.getAppDir(),filter=self._filter)
        self.label.setText(f'你选择的路径:\n{dir}')
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet